package com.zhangguo.springmvc01.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author wangxing
 * @version 2020/5/31 20:37 Administrator
 */
@Controller
@RequestMapping("/foobar")
public class FooBarController {
    private String url_index="foo/index";
    //访问路径 /foobar
    @RequestMapping
    public String action3(Model model){
        model.addAttribute("message",
                "RequestMapping 中不对value进行指定时,为当前类对应目录的起始页");
        return url_index;
    }
    //访问路径 /foobar/action2
    @RequestMapping("/action2")
    public String action2 (Model model){
        model.addAttribute("message",
                "RequestMapping 注释类和注释方法同时使用");
        return url_index;
    }
    //访问路径 /foobar/action4/1/2
    @RequestMapping("/action4/{p1}/{p2}")
    public String action4(Model model, @PathVariable int p1,@PathVariable int p2){
        model.addAttribute("message","RequestMapping " +
                "路径变量占位," +
                "使用@PathVariable注释方法参数的值绑定到URL模板变量p1+p2="
                +(p1+p2));
        return url_index;
    }
    //访问路径 /foobar/action5/12/AS or /foobar/action5/123456/asd
    @RequestMapping(value={"/action5/{id:\\d{6}}-{name:[a-z]{3}}"
            ,"/action5/{id:\\d{2}}{name:[A-Z]{2}}"})
    public String action5(Model model,@PathVariable int id,@PathVariable String name){
        model.addAttribute("message","使用正则表达式进行判定路径中数据格式" +
                "{id="+id+",name="+name+"}");
        return url_index;
    }
    //访问路径 /foobar/action6/wangxing
    @RequestMapping("/action6/{name}")
    public String action6(Model model, @PathVariable String name,
                          @MatrixVariable String r,
                          //必填 required=true
                          @MatrixVariable(required = true) String g,
                          //默认非必填
                          @MatrixVariable (required = false,defaultValue = "20")String b){
        model.addAttribute("message","@RequestMapping,矩阵变量示例," +
                "矩阵变量定义的注释@MatrixVariable,多个矩阵变量使用;或者,进行分割,矩阵变量设置为a=val;b=val2;" +
                "矩阵变量返回值:{name:"+name+",rgb="+r+","+g+","+b+"}");
        //矩阵变量需要进行修改配置SpringMVC.xml配置,默认不能使用矩阵变量
        //访问方式 http://localhost:8089/foobar/action6/wangxing;r=1;g=2
        return url_index;
    }
    //访问路径 /foobar/action7/111.do or /foobar/action7/2.dd or /foobar/action7/123/1.do
    @RequestMapping({"/action7/*.do","/action7/**/*.dd","/action7/123/?.do"})
    public String action6(Model model){
        model.addAttribute("message","@RequestMapping " +
                "使用ANT风格路径模式,支持?(单个字符),*(0或多个字符),**(0或多个路径)");
        return url_index;
    }
}
